package com.ebupt.common.net.config

import okhttp3.Cookie
import okhttp3.CookieJar
import okhttp3.HttpUrl

class LocalJar:CookieJar {

    val cookieList = mutableListOf<Cookie>()

    override fun loadForRequest(url: HttpUrl): List<Cookie> {
        val invalidCookie:MutableList<Cookie> = ArrayList()
        val validCookie:MutableList<Cookie> = ArrayList()

        for (cookie in cookieList){
            if (cookie.expiresAt<System.currentTimeMillis()){
                invalidCookie.add(cookie)
            }else if (cookie.matches(url)){
                validCookie.add(cookie)
            }
        }
        cookieList.removeAll(invalidCookie)

        return cookieList

    }

    override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
        cookieList.addAll(cookies)
    }
}